package com.example.print.fragment;


import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.print.R;
import com.example.print.activity.MainActivity;
import com.example.print.utility.CodeUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class PrintTemplateFragment extends Fragment {
    private String TAG = "PrintTemplateActivity";

    private EditText template_Paper_distance, template_chroma, text_label,text_labe2;
    private ImageView img_codes1;
    private Button btn_templateprint,btn_retteprint;

    private MainActivity mContext;
    private String barcodeData = "abcdefg1234567890ABCDEFGabcdefg1234567890ABCDEFGabcdefg1234567890ABCDEFGabcdefg1234567890ABCDEFG";
    private boolean print = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_printtemplate, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mContext = (MainActivity) getActivity();

        //走纸距离以及浓度
        text_label = mContext.findViewById(R.id.text_label);
        text_labe2 = mContext.findViewById(R.id.text_label2);
        img_codes1 = mContext.findViewById(R.id.img_codes1);
        template_Paper_distance = mContext.findViewById(R.id.template_Paper_distance);
        template_chroma = mContext.findViewById(R.id.template_chroma);
        btn_templateprint = mContext.findViewById(R.id.btn_templateprint);
        btn_retteprint = mContext.findViewById(R.id.btn_retteprint);

        barcodeData = "1234657890abcdefg0123456789ABCDEFG";
        img_codes1.setImageBitmap(CodeUtils.generateBitmap(barcodeData, 320, 320));

        StringBuilder textBuff = new StringBuilder(800);
        for (int i = 0; i < 10; i++) {
            textBuff.append(i+"ajsdlkfjsl我是卡斯蒂29102\n");
        }
        text_label.setText(textBuff.toString());

        //样板打印监听事件，功能暂未实现
        btn_templateprint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int dim = 25;
                int distance = 2;

                if(!print){
                    print = true;
                } else {
                    Toast.makeText(mContext, R.string.print_ing, Toast.LENGTH_SHORT).show();
                    return;
                }
                mContext.mPrinter.initPrint();//清空缓存区

                try {
                    String StrTemp = template_chroma.getText().toString().trim();
                    dim = Integer.parseInt(StrTemp);
                } catch (Exception e) {
                    template_chroma.setText(dim + "");
                }
                mContext.mPrinter.setPrintChromaLevel(dim);

                try {
                    String StrTemp = template_Paper_distance.getText().toString().trim();
                    distance = Integer.parseInt(StrTemp);
                } catch (Exception e) {
                    template_Paper_distance.setText(distance + "");
                }

                printTemplate();
                //打印完走纸设置的行数
                //mPrinter.setFeedRow(0,distance);

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //TODO  todo somthing here
                        print = false;
                    }
                },5 * 1000);  //延迟10秒执行
            }
        });

        btn_retteprint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                print = false;
            }
        });
    }

    private void printTemplate() {
        new Thread() {
            @Override
            public void run() {
                Log.d(TAG, "printText..");

                //mContext.mPrinter.initPrint();//清空缓存区
                //mContext.mPrinter.setPrinterStatusCallBackEnable(false);

                String testdata = text_label.getText().toString().trim();
                Log.d(TAG, "printText....testdata=" + testdata.length());

                StringBuilder textBuff = new StringBuilder(800);
                for (int i = 0; i < 10; i++) {
                    textBuff.append(i + "ajsdlkfjsl我是卡斯蒂29102\n");
                }
                testdata = textBuff.toString();
                mContext.mPrinter.printText(testdata);
                textBuff.setLength(0);
                for (int i = 0; i < 10; i++) {
                    textBuff.append(i + "ajsdlkfjsl我是卡斯蒂29102\n");
                }
                testdata = textBuff.toString();

                Bitmap bitmap = CodeUtils.generateBitmap(barcodeData, 320, 320);
                for (int i = 0; i < 5; i++) {
                    if (mContext.GetPrinterStatus()) {
                        mContext.mPrinter.printText(testdata);

                        try {
                            Thread.sleep(500);
                        } catch (Exception e) {
                        }

                        mContext.mPrinter.printImage(bitmap);
                    } else {
                        try {
                            Thread.sleep(500);
                        } catch (Exception e) {
                        }
                    }
                    mContext.mPrinter.setFeedRow(0, 5);
                }

            }
        }.start();
    }

}
